package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.pay.EntityAppConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.vo.company.GzhComListVo;
import com.ocom.company.dao.IDaoAppConfig;
import com.ocom.company.service.AppConfigService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AppConfigServiceImpl extends ServiceImpl<IDaoAppConfig, EntityAppConfig> implements AppConfigService {


    @Autowired
    IDaoAppConfig iDaoAppConfig;


    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityAppConfig> getAppConfigList(Long companyId,String appId) {
        QueryWrapper<EntityAppConfig> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        if(!CommonUtil.isNull(appId)){
            queryWrapper.eq("gzh_appid",appId);
        }
        queryWrapper.orderByDesc("update_time");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityAppConfig getAppConfig(Long companyId,String appId) {
        QueryWrapper<EntityAppConfig> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        if(!CommonUtil.isNull(appId)){
            queryWrapper.eq("gzh_appid",appId);
        }
        queryWrapper.orderByDesc("update_time");
        queryWrapper.last("limit 1");
        EntityAppConfig entityAppConfig =  this.getOne(queryWrapper);
        if(entityAppConfig==null ) return new EntityAppConfig();
        return entityAppConfig;
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    public Boolean setAppConfig(EntityAppConfig entityAppConfig) {
        UpdateWrapper<EntityAppConfig> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("com_id",entityAppConfig.getComId());
        updateWrapper.eq("gzh_appid",entityAppConfig.getGzhAppid());
        updateWrapper.set("bind",entityAppConfig.getBind());
        updateWrapper.set("register",entityAppConfig.getRegister());
        updateWrapper.set("qr_code",entityAppConfig.getQrCode());
        updateWrapper.set("recharge",entityAppConfig.getRecharge());
        updateWrapper.set("withhold_sign",entityAppConfig.getWithholdSign());
        updateWrapper.set("face_photo",entityAppConfig.getFacePhoto());
        updateWrapper.set("family",entityAppConfig.getFamily());

        updateWrapper.set("mobile",entityAppConfig.getMobile());
        updateWrapper.set("xf_basic",entityAppConfig.getXfBasic());
        updateWrapper.set("xf_balance",entityAppConfig.getXfBalance());
        updateWrapper.set("order_food",entityAppConfig.getOrderFood());
        updateWrapper.set("mj_basic",entityAppConfig.getMjBasic());
        updateWrapper.set("video_call",entityAppConfig.getVideoCall());
        updateWrapper.set("repair_pay",entityAppConfig.getRepairPay());
        updateWrapper.set("cloud_pc",entityAppConfig.getCloudPc());
        updateWrapper.set("fee",entityAppConfig.getFee());

        /**
         * 其他忽略
         */
        return this.saveOrUpdate(entityAppConfig,updateWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    public List<GzhComListVo> getCompanyListByAgentGzh(String agentAppid,Integer bingRegister) {
        if(bingRegister==1){
            return iDaoAppConfig.getCompanyListByAgentGzhBind(agentAppid,bingRegister);
        }
        if(bingRegister==2){
            return iDaoAppConfig.getCompanyListByAgentGzhRegister(agentAppid,bingRegister);
        }
        return null ;

    }
}
